<?php
namespace App\Http\Controllers\Api;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\CommentSettingTable;
use App\Model\CommentTable;
use App\Model\CouponListTable;
use App\Model\CouponTable;
use App\Model\InviteTeamTable;
use App\Model\OrderTable;
use App\Model\UserTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use App\Repository\Sys\Sys;

class CommentController extends ApiController
{


    /**
     * @api {get} /api/comment/list  评论标签列表
     * @apiGroup Wap-Comment
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "id": "1",
     * "content": "222"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getList()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 6);
        $param = [];
        $where = '1=1';
        $count = \DB::select("select count(*) as c  from comment_setting WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from comment_setting WHERE {$where} order by rand()  limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {
            $list[] = $v;
        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,
        ];
        return JsonRespose::success($data);
    }

    /**
     * @api {post} /api/comment/add  增加
     * @apiGroup Wap-Comment
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} order_id 必选
     * @apiParam {Int} team_id 应战或约战的时候有
     * @apiParam {Int} content_id 数据id,逗号隔开
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": []
     * }
     */
    public function postAdd()
    {
        $content_id = \Request::get('content_id');
        $order_id = \Request::get('order_id');
        $team_id = \Request::get('team_id', 0);


        $uid = Sys::getUid();
        if (!$uid) {
            return Sys::getUidFail();
        }
        $res = true;

        $ids = explode(',', $content_id);
        foreach ($ids as $v) {

            $content_id = $v;

            if (!$v) {
                continue;
            }


            $res = CommentTable::ORM()->where('content_id', $content_id)->where('uid', $uid)->where('order_id', $order_id)->first();
            if ($res) {
                continue;
            }
            $res = CommentTable::ORM()->where('content_id', $content_id)->where('order_id', $order_id)->first();
            if ($res) {
                $res = CommentTable::ORM()->where('content_id', $content_id)->where('order_id', $order_id)->decrement('content_num', 1);
            } else {
                $content_res = CommentSettingTable::ORM()->where('id', $content_id)->first();

                $order_res = OrderTable::ORM()->where('order_id', $order_id)->first();

                $invite_res = InviteTeamTable::ORM()->where('id', $order_res->object_id)->first();

                if ($order_res->order_type == 1) {
                    //约
                    $team_id = $invite_res->join_team_id;
                } else {
                    $team_id = $invite_res->team_id;
                }


                $res = CommentTable::ORM()->insertGetId([
                    'order_id' => $order_id,
                    'content' => $content_res->content,
                    'content_num' => 1,
                    'dateline' => time(),
                    'team_id' => $team_id,
                    'uid' => $uid,
                    'content_id' => $content_id
                ]);
            }

        }


        OrderTable::ORM()->where('order_id', $order_id)->update(['is_comment' => 1]);

        if ($res) {
            return JsonRespose::success($res);
        } else {
            return JsonRespose::fail();
        }

    }

}